//
//  _registViewController.m
//  DatePickTest
//
//  Created by user on 15/8/13.
//  Copyright (c) 2015年 user. All rights reserved.
//
#import "RegistViewController.h"
#import "RegistNextViewController.h"

@interface RegistViewController ()<UITextFieldDelegate>
@property (nonatomic,strong)UITextField * userTextField;//用户名
@property (nonatomic,strong)UITextField * psdTextField;//密码
@property (nonatomic,strong)UIView      * registView;//注册view
@property (nonatomic,strong)UIButton    * nextBtn;//下一步按钮
@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor hexStringToColor:@"#ebebeb"];
    [self createTheNavigationBar];//创建导航栏
    [self createTheUserView];//用户名及密码View
    
    //下一步按钮
    _nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(10*WSCALE, 64 + 75*HSCALE, SCREEN_WIDTH - 20*WSCALE, 35*HSCALE)];
    [_nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [_nextBtn addTarget:self action:@selector(buttonNextTapped:) forControlEvents:UIControlEventTouchUpInside];
    _nextBtn.titleLabel.font = [UIFont boldSystemFontOfSize:19];
    _nextBtn.backgroundColor = [UIColor hexStringToColor:RED_COLOR_VALUE];
    _nextBtn.layer.cornerRadius = 5;
    _nextBtn.layer.masksToBounds = TRUE;
    [self.view addSubview:_nextBtn];
    
    
    
}
#pragma mark ------------创建导航栏
-(void)createTheNavigationBar
{
    [[UIApplication sharedApplication]setStatusBarHidden:FALSE];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    
    UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 64.0f)];
    topView.backgroundColor = [UIColor hexStringToColor:COLOR_VALUE];
    [self.view addSubview:topView];
    //标题
    UILabel *titleLab = [[UILabel alloc]initWithFrame:CGRectMake(0.0f, 20.0f, SCREEN_WIDTH, 44.0f)];
    titleLab.backgroundColor = [UIColor clearColor];
    titleLab.font = [UIFont boldSystemFontOfSize:20.0f];
    titleLab.textColor = [UIColor whiteColor];
    titleLab.text = @"用户注册";
    titleLab.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:titleLab];
    
    //返回按钮
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0.0f, 20.0f, 64.0f, 44.0f)];
    
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(buttonBackTapped:) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:backBtn];
}
#pragma mark-------------填写用户信息
-(void)createTheUserView
{
    _registView = [[UIView alloc]initWithFrame:CGRectMake(0, 64 + 10*HSCALE, SCREEN_WIDTH, 40*HSCALE)];
    _registView.backgroundColor = [UIColor whiteColor];
    _registView.layer.borderWidth = 1*HSCALE;
    _registView.layer.borderColor = [RGBA(240, 240, 240, 1) CGColor];
    [self.view addSubview:_registView];
    
    UIImageView * userImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 35/2.0, 35/2.0)];
    userImageView.center = CGPointMake(35.0f/2, 20*HSCALE);
    userImageView.image = [UIImage imageNamed:@"phone"];
    [_registView addSubview:userImageView];
    
    _userTextField = [[UITextField alloc]initWithFrame:CGRectMake(40*WSCALE, 0.0f, SCREEN_WIDTH - 100*WSCALE, 40*HSCALE)];
    _userTextField.placeholder = @"输入您的手机号码";
    _userTextField.delegate = self;
    _userTextField.font = [UIFont systemFontOfSize:15.0f];
    _userTextField.keyboardType = UIKeyboardTypeNumberPad;

    [_registView addSubview:_userTextField];
    
}


#pragma mark--------------返回按钮绑定方法
-(void)buttonBackTapped:(id)sender
{
    [self dismissViewControllerAnimated:TRUE completion:nil];
}
#pragma mark-------------下一步按钮绑定方法
-(void)buttonNextTapped:(id)sender
{
    if (_userTextField.text.length<=0)
    {
        ALERT_VIEW(@"手机号码不能为空");
        return;
    }
    if ([self validateMobile:_userTextField.text] == FALSE)
    {
        ALERT_VIEW(@"手机号码不合法");
        return;
    }

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"time"])
  {
    //如果存有时间，就进行比较
    NSDate * currentDate = [NSDate date];
    if ([currentDate compare:[[NSUserDefaults standardUserDefaults] objectForKey:@"time"]] == NSOrderedAscending)
    {
        ALERT_VIEW(@"验证码已发送，时间间隔不能低于60s");
        return;
    }
  }
    [self.view endEditing:TRUE];//让键盘下去
    [MBProgressHUD showHUDAddedTo:self.view animated:TRUE];
    
    
    [AppGlobalClass PostDataWithNSstring:PHONE_ISREGIST withDictionary:@{@"mobile":_userTextField.text} success:^(NSDictionary *dictionary) {
       
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:FALSE]])
        {//未注册
            
            [AppGlobalClass PostDataWithNSstring:REGIST_CODE withDictionary:@{@"mobileNumber":_userTextField.text} success:^(NSDictionary *dictionary) {
                NSLog(@"==%@",dictionary);
                if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
                {
                    RegistNextViewController * vc = [[RegistNextViewController alloc]initWithPhoneNum:_userTextField.text];
                    [self presentViewController:vc animated:TRUE completion:^{
                        //跳转成功后，再次存时间
                        [[NSUserDefaults standardUserDefaults] setObject:[[NSDate date]dateByAddingTimeInterval:60] forKey:@"time"];
                    }];
                }
                
                [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
                
            } failure:^(NSError *error) {
                [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
                
            } networkerror:^(NSString *networkerror) {
                [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
                
            }];

        }else
        {//已注册
         
            [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
            ALERT_VIEW(@"手机号码已被注册");
        }
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
    } networkerror:^(NSString *networkerror) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:TRUE];
    }];
    
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark ----------正则表达式
//手机号码验证
-(BOOL)validateMobile:(NSString *)mobile
{
    //手机号以1开头，10个 \d 数字字符
    NSString *phoneRegex = @"^(1[0-9])\\d{9}$";
    NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}
#pragma mark--------textFieldDelegate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
        //当手机输入合法的时候，更改下一步按钮状态
        
        NSString *a = [textField.text stringByReplacingCharactersInRange:range withString:string];
        if (a.length>11) {
            
            return NO;
        }
        return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
